package com.xiaoyu.lingdian.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.xiaoyu.lingdian.service.CoreUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.xiaoyu.lingdian.core.mybatis.dao.MyBatisDAO;
import com.xiaoyu.lingdian.core.mybatis.page.Page;
import com.xiaoyu.lingdian.core.mybatis.page.PageRequest;
import com.xiaoyu.lingdian.entity.CoreUser;

@Service("coreUserService")
public class CoreUserServiceImpl implements CoreUserService {

	@Autowired
	private MyBatisDAO myBatisDAO;

	@Override
	public boolean insertCoreUser(CoreUser coreUser) {
		myBatisDAO.insert(coreUser);
		return true;
	}

	@Override
	public boolean updateCoreUser(CoreUser coreUser) {
		myBatisDAO.update(coreUser);
		return true;
	}

	@Override
	public boolean deleteCoreUser(CoreUser coreUser) {
		myBatisDAO.delete(coreUser);
		return true;
	}

	@Override
	public CoreUser getCoreUser(CoreUser coreUser) {
		return (CoreUser) myBatisDAO.findForObject(coreUser);
	}

	private static final String GET_CORE_USER_BY_NAME = "getCoreUserByName";
	private static final String GET_CORE_USER_BY_MOBILE = "getCoreUserByMobile";
	private static final String FIND_CORE_USER_BY_CND = "findCoreUserByCnd";
	private static final String DELETE_CORE_USER_BY_CND = "deleteCoreUserByCnd";
	private static final String UPDATE_CORE_USER_POINTS_ADD = "updateCoreUserPointsAdd";
	private static final String UPDATE_CORE_USER_POINTS_DESC = "updateCoreUserPointsDesc";
	private static final String UPDATE_CORE_USER_BALANCE_ADD = "updateCoreUserBalanceAdd";
	private static final String UPDATE_CORE_USER_BALANCE_DESC = "updateCoreUserBalanceDesc";
	private static final String UPDATE_CORE_USER_BY_MOBILE = "updateCoreUserByMobile";
	
	@Override
	public CoreUser getCoreUserByName(CoreUser coreUser) {
		return  (CoreUser) myBatisDAO.findForObject(GET_CORE_USER_BY_NAME, coreUser);
	}

	@Override
	public CoreUser getCoreUserByMobile(CoreUser coreUser) {
		return  (CoreUser) myBatisDAO.findForObject(GET_CORE_USER_BY_MOBILE, coreUser);
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public Page<CoreUser> findCoreUserByCnd(String crusrName,
			String crusrMobile, int pageNum, int pageSize) {
		Map<String, Object> hashMap = new HashMap<String, Object>();
		hashMap.put("crusrName", crusrName);
		hashMap.put("crusrMobile", crusrMobile);
		return myBatisDAO.findForPage(FIND_CORE_USER_BY_CND, new PageRequest(pageNum, pageSize, hashMap));
	}

	@Override
	public boolean deleteCoreUserByCnd(List<String> list) {
		Map<String, Object> hashMap = new HashMap<String, Object>();
		hashMap.put("list", list);
		myBatisDAO.delete(DELETE_CORE_USER_BY_CND, hashMap);
		return true;
	}

	@Override
	public boolean updateCoreUserPointsAdd(CoreUser coreUser) {
		myBatisDAO.update(UPDATE_CORE_USER_POINTS_ADD, coreUser);
		return true;
	}

	@Override
	public boolean updateCoreUserPointsDesc(CoreUser coreUser) {
		myBatisDAO.update(UPDATE_CORE_USER_POINTS_DESC, coreUser);
		return true;
	}

	@Override
	public boolean updateCoreUserBalanceAdd(CoreUser coreUser) {
		myBatisDAO.update(UPDATE_CORE_USER_BALANCE_ADD, coreUser);
		return true;
	}

	@Override
	public boolean updateCoreUserBalanceDesc(CoreUser coreUser) {
		myBatisDAO.update(UPDATE_CORE_USER_BALANCE_DESC, coreUser);
		return true;
	}

	@Override
	public boolean updateCoreUserByMobile(CoreUser coreUser) {	
		myBatisDAO.update(UPDATE_CORE_USER_BY_MOBILE, coreUser);
		return true;
	}	
	
}